package cn.anc.aonicardv.net.upload;

import android.os.Handler;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;

/**
 * Created by yangdai on 2017/12/2.
 */

public class UploadFileRequestBody extends RequestBody {

    private RequestBody mRequestBody;
    private UploadSubscriber upLoadSubscriber;
    private Handler handler = new Handler();

    public UploadFileRequestBody(File file, UploadSubscriber upLoadSubscriber) {
        mRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        this.upLoadSubscriber = upLoadSubscriber;
    }

    @Override
    public MediaType contentType() {
        return mRequestBody.contentType();
    }

    @Override
    public long contentLength() throws IOException {
        return mRequestBody.contentLength();
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {

        CountingSink countingSink = new CountingSink(sink);
        BufferedSink bufferedSink = Okio.buffer(countingSink);
        //写入
        mRequestBody.writeTo(bufferedSink);
        //刷新
        //必须调用flush，否则最后一部分数据可能不会被写入
        bufferedSink.flush();
    }

    protected final class CountingSink extends ForwardingSink {

        private long bytesWritten = 0;

        public CountingSink(Sink delegate) {
            super(delegate);
        }


        @Override
        public void write(Buffer source, long byteCount) throws IOException {
            super.write(source, byteCount);
            bytesWritten += byteCount;
            if (upLoadSubscriber != null) {
                if (bytesWritten != contentLength()) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                upLoadSubscriber.onProgress(bytesWritten, contentLength());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            upLoadSubscriber.onSuccess();
                        }
                    });
                }
            }
        }

    }
}
